<?php
namespace zake\server\zadb;
use zake\foundation\point\Table;

/**
 * Class Za
 * @package zake\server\zadb
 *
 * zadb 客户端
 */
class ZaClient
{
    /**
     * @var ZaDb | null
     */
    private static $singleton = null;


    /**
     * @var ZaConnect|null
     */
    private $connect = null;



    private $protocol = null;

    /**
     * 构造器
     *
     * @author liu.bin 2020-04-03 23:16
     */
    public static function singleton()
    {
        if(is_null(static::$singleton)){
            static::$singleton = new static();
        }
        return static::$singleton;
    }


    /**
     * ZaClient constructor.
     * 构造器
     */
    private function __construct()
    {
        // socket链接
        $uri            = ZaListen::URI;
        $resource       = @stream_socket_client($uri, $errNo, $errStr);
        $this->connect  = ZaConnect::create($resource);
        $this->protocol = Table::singleton();
    }


    /**
     * 获取列表记录
     * @param $name
     * @param array $default
     *
     * @return array
     * @author liu.bin 2020-05-19 13:40
     */
    public function lists($name,$default=[])
    {
        $protocol = $this->protocol;
        $frame = $this->connect->read($protocol);
        return json_decode($frame,true);
    }


    /**
     * 获取同步记录
     * @param $name
     * @param $id
     * @param array $default
     *
     * @author liu.bin 2020-05-19 13:40
     */
    public function find($name,$id,$default=[])
    {

    }

    /**
     * 删除记录
     * @param $name
     * @param $id
     *
     * @author liu.bin 2020-05-19 13:40
     */
    public function delete($name,$id)
    {

    }


    /**
     * 修改记录
     * @param $name
     * @param $id
     * @param $body
     *
     * @author liu.bin 2020-05-19 13:40
     */
    public function modify($name,$id,$body)
    {

    }
}
